<?php

if (!defined("DAO_VOCLASS_PATH")) {
    die("no define DAO_VOCLASS_PATH");
}

/**
 *
 * [2012/03/25][Milo Liu] Add following features in 1.3 branch
 * 1. Allow set the master/slaver type for a DB connection
 * 2. add the charater setting into the configuration of each DB connection
 */
define('DB_LB_MASTER', 'master');
define('DB_LB_SLAVER', 'slaver');

abstract class DBConfig {
    private static $_dbConfigData;
    private static $_errorMessage;

    private static $_syncFequence = 5; // 5 seconds

    public static function getDBConfig($key) {
        if(array_key_exists($key, self::$_dbConfigData)){
            return self::$_dbConfigData[$key];
        }else{
            return null;
        }
    }

    public static function setSyncFequence($sec){
        self::$_syncFequence = $sec;
    }

    public static function getSyncFequence(){

        return self::$_syncFequence;
    }

    /**
     * Keep the function definition of old version(v1.2)
     * ------------------------------------------------- 
     * public static function setDBConfig($key, $dbHost, $dbUser, $dbPsw, $dbName) {
     *
     * Two new parameters are added in this verion:
     * @param charset  the character code for the conntion, the default value is 'utf8'
     * @param lb_type  the load balance type for the connection, default value is DB_LB_MASTER => 'master', also could set as DB_LB_SLAVER => 'slaver'
     */  
    public static function setDBConfig($key, $dbHost, $dbUser, $dbPsw, $dbName, $charset='utf8', $lb_type=DB_LB_MASTER) {
        self::$_dbConfigData[$key][$lb_type][] = array(
            "DB_HOST" => $dbHost
            ,"DB_USER" => $dbUser
            ,"DB_PSW" => $dbPsw
            ,"DB_NAME" => $dbName
            ,"DB_CHARSET" => $charset
        );
    }


    /**
     * This method is used to check if all items of the configuration are set completely
     */
    public static function check($key){
        //check if the mater db is set
        //check the required elements are provided in right format
        $conf = self::getDBConfig($key);

        if(null == $conf){
            self::$_errorMessage = 'No Database Config for the config key: '. $key;
            return false;
        }

        if( !array_key_exists(DB_LB_MASTER, $conf) 
            || count($conf[DB_LB_MASTER]) == 0 ){

                self::$_errorMessage = 'No Mater DB setting for the config key: '. $key;
                return false;
            }

        if(!self::checkConf($conf[DB_LB_MASTER][0]){
            self::$_errorMessage .= ' for the master db for the config key: '. $key;
            return false;
        }

        foreach($conf[DB_LB_SLAVER] as $sIndex => $sConf){
            
            if(!self::checkConf($sConf)){

                self::$_errorMessage .= ' for the No. ' $sIndex . ' slaver db for the config key: '. $key;
                return false;
            }
        }

        return true;
    }


    /**
     * This method is used to check the single connection configuration
     */
    public static function checkConf($conf){
        if(! array_key_exists('DB_HOST', $conf)){
            self::$_errorMessage = 'The host of the db is not set';
            return false;
        }

        if(! array_key_exists('DB_NAME', $conf)){
            self::$_errorMessage = 'The db name is not set';
            return false;
        }

        if(! array_key_exists('DB_USER', $conf)){
            self::$_errorMessage = 'No user provided for accessing the db';
            return false;
        }

        return true;
    }

    public static function getErrorMessage(){
        return self::$_errorMessage;
    }
}
